package com.wanmait.recycle.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wanmait.recycle.pojo.Admin;
import com.wanmait.recycle.service.AdminService;
import com.wanmait.recycle.util.ValidateCode;

@Controller()
@RequestMapping("/manager")
public class LoginController {

	@Resource
	private AdminService adminService;
	
	@RequestMapping("login")
	public void login() {
		
	}
	/*生成验证码*/
	@RequestMapping("checkCode")
	public void creatCheckCode(HttpServletRequest request,HttpServletResponse response) throws Exception {
		
		ValidateCode vCode = new ValidateCode();  
		String code = vCode.getCode();
		HttpSession session = request.getSession();
		session.setAttribute("code", code);
		vCode.write(response.getOutputStream());
	}
	//判断验证码是否正确
		@RequestMapping("checkCodeIsTrue")
		@ResponseBody
		public String checkCodeIsTrue(HttpServletRequest request,String checkCode) {
			
			String flag = "";
			HttpSession session = request.getSession();
			String code = (String)session.getAttribute("code");
			if(code.equalsIgnoreCase(checkCode)) {
				flag = "isTrue";
			}
			return flag;
		}
	
	/*点击登录*/
	@RequestMapping("loginSubmit")
	public String loginSubmit(String name,String password,HttpServletRequest request) {
		
		Admin admin = adminService.findAdminByNameAndPassword(name, password);
		if(admin!=null&&admin.getPassword().equals(password)) {
			HttpSession session = request.getSession();
			session.setAttribute("admin", admin);
			return "redirect:/manager/managerIndex";
		}else {
			return "redirect:/manager/login?mes=error";
		}
	}
	/*点击退出*/
	@RequestMapping("exit")
	public String exit(HttpServletRequest request) {
	
		HttpSession session = request.getSession();
		session.removeAttribute("admin");
		return "/manager/login";
	}
	
	
}
